package com.activenetwork.smlite.business;

import org.openqa.selenium.WebElement;

import com.activenetwork.schema.model.vendor.EPVendorSearchType;
import com.activenetwork.smlite.page.rfp.RfpMaintenanceView.RfpVendorSearch;
import com.activenetwork.util.PageLoader;

public class VendorSearchBusiness {
	
	public void searchEPVendor(EPVendorSearchType vendorSearch){
		RfpVendorSearch rfpVendorSearch = PageLoader.load(RfpVendorSearch.class);
		EPVendorSearchType.Condition condition = vendorSearch.getCondition();
		rfpVendorSearch.setLocationCondition(condition.getLocation());
		rfpVendorSearch.setVenueCondition(condition.getVenue());
		rfpVendorSearch = rfpVendorSearch.clickGo();
	}
	
	public void selectEPVendor(EPVendorSearchType vendorSearch){
		// search progress
		searchEPVendor(vendorSearch);
		// select progress
		RfpVendorSearch rfpVendorSearch = PageLoader.load(RfpVendorSearch.class);
		// loop to check the vendors on one page
		while(true){
			rfpVendorSearch = rfpVendorSearch.bookVendorByDataObject(vendorSearch);
			// if the vendor name is displayed in chart, break;
			if(rfpVendorSearch.isVendorAdded(vendorSearch.getResult().getVendorName())){
				break;
			}
			// if no next page , break
			WebElement nextPageLink = rfpVendorSearch.getNextPageLink();
			if(nextPageLink == null){
				break;
			}else{
				nextPageLink.click();	
				// Check if the loading ... layer display
				rfpVendorSearch =  rfpVendorSearch.waitForProgressBar();
			}
		}
	}

}
